<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>模拟注册</title>
  <style>
    span {
      color: red;
      font-weight: bolder;
    }
  </style>
</head>

<body>
  <form action="user/register" method="post">
    <input type="text" id="username" name="username" placeholder="username" required /> <span></span><br /><br />
    <input type="password" id="password" name="password" placeholder="password" required /><br /><br />
    <input type="password" id="confirmPassword" name="confirmPassword" placeholder="confirmPassword" required /><br /><br />
    <input type="text" id="email" name="email" placeholder="email" required /><br /><br />
    <input type="submit" id="submitBtn" value="register">
  </form>

  <script>
    let isOK = true
    //用户名验证
    username.onkeyup = function () {
      if (/^[A-Z][A-Za-z]{3,}$/.test(this.value)) {
        username.nextElementSibling.innerHTML = ''
        isOK = true
      } else {
        username.nextElementSibling.innerHTML = '用户名格式不合法!'
        isOK = false
      }
    }

    submitBtn.onclick = function () {
      if (!isOK) {
        return false
      }
    }
  </script>
</body>

</html>